package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.databinding.FinanceflowdetailsActivityBinding;
import com.sgb.capital.model.BalanceDetailsEntity;
import com.sgb.capital.model.Bean;
import com.sgb.capital.model.RecordsBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.ui.adapter.BalanceDetailsAdapter;
import com.sgb.capital.viewmodel.FinanceFlowModel;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/4/19 0019
 * 说明:财务流水详情
 */


public class FinanceFlowDetailsActivity extends BaseActivity {
    private FinanceflowdetailsActivityBinding mBinding;
    private List<Bean> mBeans = new ArrayList<>();
    private BalanceDetailsAdapter mAdapter;
    private RecordsBean mPayFlowBean;
    FinanceFlowModel mModel;
    private BalanceDetailsEntity mDetailsEntity;

    public static void start(Context context, String data, String flowNo) {
        Intent intent = new Intent(context, FinanceFlowDetailsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("data", data);
        intent.putExtra("flowNo", flowNo);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.financeflowdetails_activity);
        if (Utils.isEmpty(getIntent().getStringExtra("flowNo"))) {
            String data = getIntent().getStringExtra("data");
            mPayFlowBean = new Gson().fromJson(data, RecordsBean.class);
        }
        mBinding.rv.setLayoutManager(new LinearLayoutManager(Utils.getContext()));
        mAdapter = new BalanceDetailsAdapter(null);
        mBinding.rv.setAdapter(mAdapter);
        mBinding.ivBack.setOnClickListener(v -> finish());
    }


    @Override
    public void initObserve() {
        mModel.mData.observe(this, balanceDetailsEntity -> {
            mDetailsEntity = balanceDetailsEntity;
            mBinding.ivShow.setVisibility(balanceDetailsEntity.repeatPayStatus == 2 ? View.VISIBLE : View.GONE);
            if (balanceDetailsEntity.repeatPayStatus == 3) {
                mBinding.ivShow.setImageResource(R.mipmap.yichangdingdan);
                mBinding.ivShow.setVisibility(View.VISIBLE);
            }
            mBinding.ivIcon.setImageResource(mDetailsEntity.tradeStatus == 1 ? R.mipmap.succeed_icon : R.mipmap.djz);
            mBinding.tvTip.setText(mDetailsEntity.tradeStatus == 1 ? "已完成" : "冻结中");
            // 	交易类型 1.订单交易,2.转账汇款,3.账户提现,4.订单退款
            mBeans.add(new Bean("交易类型",
                    mDetailsEntity.sourceType.contains("1") ? "订单交易" :
                            mDetailsEntity.sourceType.contains("2") ? "转账汇款" :
                                    mDetailsEntity.sourceType.contains("3") ? "账户提现" : "订单退款"
            ));
            if (!mDetailsEntity.sourceType.contains("2")) {
                mBeans.add(new Bean("业务类型", mDetailsEntity.sourceType.contains("3") ? "账户提现" : mDetailsEntity.sourceType.contains("2") ? "在线转账" : mDetailsEntity.tradeType));
            }
            if (!Utils.isEmpty(mDetailsEntity.payType)) {
                mBeans.add(new Bean("支付方式", mDetailsEntity.payType));
            }
            mBeans.add(new Bean((mDetailsEntity.sourceType.contains("1") || mDetailsEntity.sourceType.contains("2")) ? "交易金额" : "订单金额", (mDetailsEntity.tradeFlowType == 1 ? "+" : "-") + Utils.getDecimalFormat(Float.parseFloat(mDetailsEntity.tradeAmount))));
            if (!mDetailsEntity.payType.contains("余额支付") && !mDetailsEntity.sourceType.contains("2")) {
                mBeans.add(new Bean("交易手续费", "-" + Utils.getDecimalFormat(mDetailsEntity.tradeFees)));
            } else {
                if (mDetailsEntity.tradeFlowType == 1) {
                    mBeans.add(new Bean("交易手续费",
                           mDetailsEntity.tradeFees== 0 ? "/" : "-" + Utils.getDecimalFormat(mDetailsEntity.tradeFees)));
                }
            }
            if (mDetailsEntity.tradeType.contains("余额充值")) {
                if (!Utils.isEmpty(mDetailsEntity.balance)) {
                    mBeans.add(new Bean("账户余额", Utils.getDecimalFormat(Float.parseFloat(mDetailsEntity.balance))));
                } else {
                    mBeans.add(new Bean("账户余额", "/"));
                }
                mBeans.add(new Bean("交易状态", (mDetailsEntity.tradeStatus == 1 ? "已完成" : "冻结中")));
                if (mDetailsEntity.tradeFlowType != 1 && !Utils.isEmpty(mDetailsEntity.payChannels)) {
                    mBeans.add(new Bean("付款渠道", mDetailsEntity.payChannels));
                }
                if (!Utils.isEmpty(mDetailsEntity.flowNo)) {
                    mBeans.add(new Bean("交易流水号", mDetailsEntity.flowNo));
                }
                if (!Utils.isEmpty(mDetailsEntity.orderId)) {
                    mBeans.add(new Bean("订单编号", mDetailsEntity.paymentId));
                }
                mBeans.add(new Bean("余额充值时间", Utils.getTime(mDetailsEntity.created)));
                if (!Utils.isEmpty(mDetailsEntity.retMsg)) {
                    mBeans.add(new Bean("余额充值时间", Utils.getTime(mDetailsEntity.created)));
                    mBeans.add(new Bean("失败原因", mDetailsEntity.retMsg));
                    mBeans.add(new Bean("退款渠道", Utils.isEmpty(mDetailsEntity.withdrawBankAccount) ? "原路退回" : "原路退回(" + mDetailsEntity.withdrawBankAccount + ")"));
                }
            } else if (mDetailsEntity.payType.contains("余额支付")) {
                mBeans.add(new Bean("交易状态", (mDetailsEntity.tradeStatus == 1 ? "已完成" : "冻结中")));
                if (!Utils.isEmpty(mDetailsEntity.payName)) {
                    mBeans.add(new Bean("付款方", mDetailsEntity.payName.length() >= 12 ? mDetailsEntity.payName.substring(12) + "..." : mDetailsEntity.payName));
                }
                if (!Utils.isEmpty(mDetailsEntity.payeeName)) {
                    mBeans.add(new Bean("收款方", mDetailsEntity.payeeName.length() >= 12 ? mDetailsEntity.payeeName.substring(12) + "..." : mDetailsEntity.payeeName));
                }
                if (!Utils.isEmpty(mDetailsEntity.flowNo)) {
                    mBeans.add(new Bean("交易流水号", mDetailsEntity.flowNo));
                }
                if (!Utils.isEmpty(mDetailsEntity.paymentId)) {
                    mBeans.add(new Bean("订单编号", mDetailsEntity.paymentId));
                }
                if (!Utils.isEmpty(mDetailsEntity.orderId) && !mDetailsEntity.sourceType.contains("2")) {
                    mBeans.add(new Bean("业务支付号", mDetailsEntity.orderId));
                }
                mBeans.add(new Bean("创建时间", Utils.getTime(mDetailsEntity.created)));
                if (mDetailsEntity.payTime != 0) {
                    mBeans.add(new Bean("付款时间", Utils.getTime(mDetailsEntity.payTime)));
                }
                if (!Utils.isEmpty(mDetailsEntity.finishTime)) {
                    mBeans.add(new Bean("解冻时间", Utils.getTime(Long.parseLong(mDetailsEntity.finishTime))));
                }
            } else {
                mBeans.add(new Bean("交易状态", (mDetailsEntity.tradeStatus == 1 ? "已完成" : "冻结中")));
                if (!Utils.isEmpty(mDetailsEntity.payName)) {
                    mBeans.add(new Bean("付款方", mDetailsEntity.payName));
                }
                if (mDetailsEntity.tradeFlowType != 1 && !Utils.isEmpty(mDetailsEntity.payChannels) && (mDetailsEntity.payType.contains("快捷") || mDetailsEntity.payType.contains("B2B"))) {
                    mBeans.add(new Bean("付款渠道", mDetailsEntity.payChannels));
                }
                if (!Utils.isEmpty(mDetailsEntity.payeeName)) {
                    mBeans.add(new Bean("收款方", mDetailsEntity.payeeName));
                }
                if (!Utils.isEmpty(mDetailsEntity.payAccountName)) {
                    mBeans.add(new Bean("付款账户", mDetailsEntity.payAccountName));
                }
                if (!Utils.isEmpty(mDetailsEntity.payAccountNumber)) {
                    mBeans.add(new Bean("付款账号", mDetailsEntity.payAccountNumber));
                }
                if (!Utils.isEmpty(mDetailsEntity.flowNo)) {
                    mBeans.add(new Bean("交易流水号", mDetailsEntity.flowNo));
                }
                if (!Utils.isEmpty(mDetailsEntity.paymentId)) {
                    mBeans.add(new Bean("订单编号", mDetailsEntity.paymentId));
                }
                if (!Utils.isEmpty(mDetailsEntity.orderId) && !mDetailsEntity.sourceType.contains("2")) {
                    mBeans.add(new Bean("业务支付号", mDetailsEntity.orderId));
                }
                mBeans.add(new Bean("创建时间", Utils.getTime(mDetailsEntity.created)));
                if (mDetailsEntity.payTime != 0) {
                    mBeans.add(new Bean("付款时间", Utils.getTime(mDetailsEntity.payTime)));
                }
                if (!Utils.isEmpty(mDetailsEntity.finishTime)) {
                    mBeans.add(new Bean("解冻时间", Utils.getTime(Long.parseLong(mDetailsEntity.finishTime))));
                }
            }
            mAdapter.setNewInstance(mBeans);
            mBinding.view.setVisibility(View.VISIBLE);
        });
    }

    @Override
    public void initData() {
        mModel = new ViewModelProvider(this).get(FinanceFlowModel.class);
        if (Utils.isEmpty(getIntent().getStringExtra("flowNo"))) {
            mModel.getPayFlowDetails(this, mPayFlowBean.flowNo);
        } else {
            mModel.getPayFlowDetails(this, getIntent().getStringExtra("flowNo"));
        }
    }
}